package org.ayachinene.leetcode.problems;

import org.ayachinene.leetcode.structure.ListNode;

public class P206 {

    static class Solution {

        /**
         * 反转head链表
         */
        public ListNode reverseList(ListNode head) {
            // 空链表和只含一个元素的链表不需要反转
            if (head == null || head.next == null) {
                return head;
            }

            ListNode next = head.next;
            ListNode newHead = reverseList(next);  // 反转后next结点转移到链表末尾
            next.next = head;
            head.next = null;
            return newHead;
        }
    }
}
